package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.hnu.qq.entity.ChartContent;

public class DownloadListDialog extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel(){
		@Override
		protected void paintComponent(Graphics g) {
			Image image = null;
			try {
				image = ImageIO.read(new File("src/main/java/image/02.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			};
					
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

		}
	};
	private JList<ChartContent>  list;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DownloadListDialog dialog = new DownloadListDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DownloadListDialog() {
		init();
	}

	public DownloadListDialog(Dialog owner, boolean modal) {
		super(owner, modal);
		init();
	}

	private void init() {
		try {
			setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JLabel label = new JLabel("聊天记录：");
			contentPanel.add(label, BorderLayout.NORTH);
		}
		{
			list = new JList<ChartContent>();
			contentPanel.add(list, BorderLayout.CENTER);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
				cancelButton.addActionListener(this);
			}
		}
	}

	public void setList(List<ChartContent> answer) {
		DefaultListModel<ChartContent> dm = new DefaultListModel<ChartContent>();
		for(ChartContent c:answer){
			dm.addElement(c);
		}
		list.setModel(dm);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("OK")){
			ChartContent c = list.getSelectedValue();
			if(c == null){
				JOptionPane.showMessageDialog(this, "你没有选择记录");
				return;
			}else{
				String filename = null;
				if(c.getCrowd() == null){
					filename = c.getOwner()+"_"+c.getCharter()+"_"+new SimpleDateFormat("yyyy_MM_DD_HH_mm_ss").format(c.getTime());
				}else{
					filename = c.getOwner()+"_"+c.getCrowd()+"_"+new SimpleDateFormat("yyyy_MM_DD_HH_mm_ss").format(c.getTime());
				}
				String dir =  "D://chart content";
				File f = new File(dir);
				if(!f.exists()){
					f.mkdir();
				} 
				try {
					DataOutputStream dout = new DataOutputStream(new FileOutputStream(new File(dir+"/"+filename)));
					dout.writeUTF(c.getContent());
					dout.close();
					JOptionPane.showMessageDialog(this, "记录已经成功保存至："+f.getAbsolutePath());
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(this, "记录保存出错，请稍后再试");
					e1.printStackTrace();
				}
			}
		}
		if(e.getActionCommand().equals("Cancel")){
			this.dispose();
		}
	}

}
